package com.gyms.base;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.gyms.MyApplication;
import com.gyms.R;
import com.gyms.activity.CodeLoginActivity;
import com.gyms.constant.Constants;
import com.gyms.manager.DataCenter;
import com.gyms.manager.TitleManager;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import utils.StringUtils;
import weight.CommonEmptyView;
import weight.loadingview.DialogUtil;

import static com.gyms.manager.TitleManager.TitleTheme.Blue;

/**
 * ltx
 */
public abstract class BaseFragment extends Fragment {

    public View mRootView;
    private Unbinder mUnbinder;
    private ProgressDialog progressDialog;
    public Context mContext;
    public MyApplication mMyApplication;


    protected TitleManager mTitleManager;//用title管理类处理
    protected TitleManager.TitleTheme mTheme = Blue;

    //加载中等待提示
    protected Dialog loadingView = null;

    protected final static int PAGESIZE = 10;
    protected int mPageIndex = 1;

    protected int mIsRefreshOrLoadMore = 0;//0是刷新1是加载更多

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        mRootView = initView(inflater,container);
        mRootView = LayoutInflater.from(getActivity()).inflate(getLayoutResId(), container, false);
        mUnbinder = ButterKnife.bind(this, mRootView);//绑定到butterknife
        mContext = getActivity();
        mMyApplication = ((MyApplication) getActivity().getApplication()).getInstance();
        return mRootView;
    }

    /**
     * 设置framge按键监听
     *
     * @param listener
     */
    protected void setOnKeyDown(View.OnKeyListener listener) {
        mRootView.setOnKeyListener(listener);
    }

    /**
     * 自定义等待框提示
     *
     * @param text
     */
    protected Dialog createLoadingView(String text, Boolean canceledOnTouchOutside) {
        loadingView = DialogUtil.createLoadingDialog(mContext, text, canceledOnTouchOutside);
        return loadingView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
     /*   onFirst();*/
        setTitleTheme();
        if (null != mRootView.findViewById(R.id.tv_title)) {
            mTitleManager = new TitleManager(mRootView, mContext, mTheme);//初始化标题栏
        }
        initView();
        initListener();
        processLogic();
    }

    @Override
    public void onStop() {
        super.onStop();
        if (loadingView != null) {
            loadingView.dismiss();
            loadingView = null;
        }
    }

    /*********************
     * 子类实现
     *****************************/
    protected abstract int getLayoutResId();//设置资源

    protected abstract void initView();//初始化控件

    protected abstract void initListener();//监听事件

    protected abstract void processLogic();//初始化数据

    /**
     * 设置标题配色
     */
    protected void setTitleTheme() {

    }

    /**
     * 通过Class跳转界面
     * 是否需要销毁当前页面
     **/
    public void startActivity(Class<?> cls, boolean isFinish) {
        startActivity(cls, null, isFinish);
    }


    /**
     * 是否登录判断跳转，并跳转到首页
     *
     * @param cls
     * @param isToMain 是否从主页进入
     */
    public void startActivityIsLogin(Class<?> cls, boolean isToMain) {
        if (!DataCenter.getInstance().checkIsLogin()) {
            Intent intent = new Intent(mContext, CodeLoginActivity.class);
            intent.putExtra("isToMain", isToMain);
            startActivity(intent, false);
        } else {
            startActivity(cls, null, false);
        }
    }

    /**
     * 是否登录判断跳转
     *
     * @param cls
     */
    public void startActivityIsLogin(Class<?> cls,boolean isToMain, Bundle bundle) {
        if (!DataCenter.getInstance().checkIsLogin()) {
            Intent intent = new Intent(mContext, CodeLoginActivity.class);
            intent.putExtra("isToMain", isToMain);
            startActivity(intent, false);
        } else {
            startActivity(cls, bundle, false);
        }
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle, boolean isFinish) {
        Intent intent = new Intent();
        intent.setClass(getContext(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        if(CodeLoginActivity.class.getSimpleName().equals(cls.getSimpleName())) {
            getActivity().overridePendingTransition(R.anim.activity_open, R.anim.activity_stay);
        }else {
            getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }
        if (isFinish) {
            getActivity().finish();
        }
    }

    /**
     * 含有Intent跳转界面
     **/
    public void startActivity(Intent intent, boolean isFinish) {
        startActivity(intent);
        if(CodeLoginActivity.class.getName().equals(intent.getComponent().getClassName())){
            getActivity().overridePendingTransition(R.anim.activity_open, R.anim.activity_stay);
        }else {
            getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }
        if (isFinish) {
            getActivity().finish();
        }
    }


    /**
     * 设置标题
     *
     * @param title 标题
     */
    public void setTitle(String title) {
        mTitleManager.setTitle(title);
    }

    /**
     * 返回键
     */

    public void setBack() {
        ImageView mBack = (ImageView) mRootView.findViewById(R.id.iv_back);
        mBack.setVisibility(View.GONE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }

    /**
     * dialog dismiss
     */
    public void dialogDis() {
        if (null != loadingView) {
            loadingView.dismiss();
        }
    }

    /**
     * dialog & CommonEmptyView dismiss
     */
    public void dialogDis(View contentView,CommonEmptyView mSearchEmpty) {
        dismissEmptyView(contentView,mSearchEmpty);
        if (null != loadingView) {
            loadingView.dismiss();
        }
    }

    /**
     * 显示空界面或错误界面
     * @param mSearchEmpty
     * @param code  非-100为无内容界面，否则为无网络界面
     */
    protected void showEmptyView(View contentView,CommonEmptyView mSearchEmpty,int code) {
        if(!StringUtils.isNull(mSearchEmpty)) {
            if (code == Constants.NO_NET) {//无网络的状态
                mSearchEmpty.setImgContent(Constants.TYPE_FAIL);
                mSearchEmpty.setVisibility(View.VISIBLE);
                contentView.setVisibility(View.GONE);
            } else {
                mSearchEmpty.setVisibility(View.VISIBLE);//空页面出现
                contentView.setVisibility(View.GONE);
                mSearchEmpty.setFailText(Constants.SEARCH_FAIL_TEXT);
                mSearchEmpty.setImgContent(Constants.TYPE_NOME);
            }
        }
    }

    /**
     * 隐藏空界面
     *
     * @param mSearchEmpty 传自定义界面
     */
    protected void dismissEmptyView(View contentView,CommonEmptyView mSearchEmpty) {
        if(!StringUtils.isNull(mSearchEmpty)) {
            contentView.setVisibility(View.VISIBLE);
            mSearchEmpty.setVisibility(View.GONE);//空页面隐藏
        }
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    public void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }
}
